package cn.ratel.component;

import cn.ratel.route.ContextHolder;
import cn.ratel.route.Controller;
import com.google.common.base.Preconditions;

import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.Map;

public class Routes {
	private static Map<String, Controller> controllerMap = new HashMap<>();

	private Routes() {
	}

	private static Routes me = new Routes();

	public static Routes me() {
		return me;
	}

	public static void init(ServletContext servletContext) {
		ContextHolder.getServletContext().set(servletContext);
	}

	public Controller getController(String controllerName) {
		return controllerMap.get(controllerName);
	}

	public void addController(String controllerName, Class<?> clazz) {
		Preconditions.checkArgument(controllerMap.get(controllerName) == null, "controllerName path is already exist :" + controllerName);
		controllerMap.put(controllerName, new Controller(controllerName, clazz));
	}

}
